<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="utf-8">
  <title>Slides &laquo; Admin</title>
  <link rel="stylesheet" href="../assets/vendors/bootstrap/css/bootstrap.css">
  <link rel="stylesheet" href="../assets/vendors/font-awesome/css/font-awesome.css">
  <link rel="stylesheet" href="../assets/vendors/nprogress/nprogress.css">
  <link rel="stylesheet" href="../assets/css/admin.css">
  <script src="../assets/vendors/nprogress/nprogress.js"></script>
</head>

<body>
  <script>
    NProgress.start()
  </script>

  <div class="main">
    <nav class="navbar">
      <button class="btn btn-default navbar-btn fa fa-bars"></button>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="profile.html"><i class="fa fa-user"></i>个人中心</a></li>
        <li><a href="login.html"><i class="fa fa-sign-out"></i>退出</a></li>
      </ul>
    </nav>
    <div class="container-fluid">
      <div class="page-title">
        <h1>图片轮播</h1>
      </div>
      <!-- 有错误信息时展示 -->
      <div class="alert alert-danger" id="err" style="display:none">
        <strong>错误！</strong><span id="error"></span>
      </div>
      <div class="row">
        <div class="col-md-4">
          <form id="addlbt">
            <h2>添加新轮播内容</h2>
            <div class="form-group">
              <label for="image">图片</label>
              <!-- show when image chose -->
              <img class="help-block thumbnail" style="display: none">
              <input id="image" class="form-control" name="image" type="file">
              <input type="hidden" name="image" id="hiddenimg">
            </div>
            <div class="form-group">
              <label for="text">文本</label>
              <input id="text" class="form-control" name="title" type="text" placeholder="文本">
            </div>
            <div class="form-group">
              <label for="link">链接</label>
              <input id="link" class="form-control" name="link" type="text" placeholder="链接">
            </div>
            <div class="form-group">
              <button class="btn btn-primary" type="submit">添加</button>
            </div>
          </form>
        </div>
        <div class="col-md-8">
          <table class="table table-striped table-bordered table-hover">
            <thead>
              <tr>
                <th class="text-center">图片</th>
                <th>文本</th>
                <th>链接</th>
                <th class="text-center" width="100">操作</th>
              </tr>
            </thead>
            <tbody id="wzlist">

            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>

  <div class="aside">
    <div class="profile">
      <img class="avatar" src="../uploads/avatar.jpg">
      <h3 class="name">Coder</h3>
    </div>
    <ul class="nav">
      <li>
        <a href="index.html"><i class="fa fa-dashboard"></i>仪表盘</a>
      </li>
      <li>
        <a href="#menu-posts" class="collapsed" data-toggle="collapse">
          <i class="fa fa-thumb-tack"></i>文章<i class="fa fa-angle-right"></i>
        </a>
        <ul id="menu-posts" class="collapse">
          <li><a href="posts.html">所有文章</a></li>
          <li><a href="post-add.html">写文章</a></li>
          <li><a href="categories.html">分类目录</a></li>
        </ul>
      </li>
      <li>
        <a href="comments.html"><i class="fa fa-comments"></i>评论</a>
      </li>
      <li>
        <a href="users.html"><i class="fa fa-users"></i>用户</a>
      </li>
      <li class="active">
        <a href="#menu-settings" data-toggle="collapse">
          <i class="fa fa-cogs"></i>设置<i class="fa fa-angle-right"></i>
        </a>
        <ul id="menu-settings" class="collapse in">
          <li class="active"><a href="slides.html">图片轮播</a></li>
          <li><a href="settings.html">网站设置</a></li>
        </ul>
      </li>
    </ul>
  </div>

  <script src="../assets/vendors/jquery/jquery.js"></script>
  <script src="../assets/vendors/bootstrap/js/bootstrap.js"></script>
  <script src="../assets/vendors/art-template/template-web.js"></script>
  <script type="text/html" id="addwzbox">
    {{ each data v }}
    <tr>
      <td class="text-center"><img class="slide" src="{{ v.image }}"></td>
      <td>{{ v.title }}</td>
      <td>{{ v.link }}</td>
      <td class="text-center">
        <a href="javascript:;" class="btn btn-danger btn-xs delBtn" data-id="{{ v._id }}"
          data-title="{{ v.title }}">删除</a>
      </td>
    </tr>
    {{ /each }}
  </script>

  <script>
    NProgress.done()
  </script>
</body>
<script>
  $(() => {

    $.ajax({
      type: 'get',
      url: '/slides',
      success: (res) => {
        console.log(res);
        $('#wzlist').html(template('addwzbox', {
          data: res
        }))
      }
    })

    $('#addlbt').on('submit', function () {
      console.log(1);
      var formdata = $(this).serialize();
      $.ajax({
        type: 'post',
        url: '/slides',
        data: formdata,
        success: (res) => {
          alert('添加成功')
          location.reload();
        },
        error: () => {
          $('#err').show()
          $('#error').html('添加失败')
        }
      })
      return false
    })

    $('#image').on('change', function () {
      var formData = new FormData()
      console.log(this.files[0]);
      formData.append('avatar', this.files[0])
      $.ajax({
        type: "post",
        url: '/upload',
        data: formData,
        processData: false,
        contentType: false,
        success: function (res) {
          $('.thumbnail').attr('src', res[0].avatar).show()
          $('#hiddenimg').val(res[0].avatar)
        }
      });

    })


    $('#wzlist').on('click', '.delBtn', ({
      target
    }) => {
      if (!confirm(`你确定要删除 "${target.dataset.title}" 吗？`)) return
      $.ajax({
        type: 'DELETE',
        url: `/slides/${target.dataset.id}`,
        success() {
          alert('删除成功')
          location.reload();
        }
      })

    })
  })
</script>

</html>
